<?php

/**
 * SchemaForm
 */
class SchemaForm extends CFormModel
{
	public $charset = 'utf-8';
	public $projectName;
	public $indexServer = '8383';
	public $searchServer = '8384';

	/**
	 * @var FieldForm[]
	 */
	private $_fields = array();

	public function getFields()
	{
		return $this->_fields;
	}

	public function setFields($data)
	{
		if (is_array($data)) {
			foreach ($data as $value) {
				$ff = new FieldForm;
				$ff->attributes = $value;
				if ($ff->validate()) {
					$this->_fields[] = $ff;
				}
			}
		}
	}

	public function rules()
	{
		return array(
			array('projectName, indexServer, searchServer', 'required'),
			array('charset, fields', 'safe'),
			array('fields', 'checkSchema'),
		);
	}

	public function checkSchema($attribute, $params)
	{
		if (count($this->$attribute) > 254) {
			return $this->addError($attribute, '字段数量不能超过 254 个');
		}
		$hasId = $hasTitle = $hasBody = false;
		foreach ($this->$attribute as $tmp) {/* @var $tmp FieldForm */
			switch ($tmp->type) {
				case 'id':
					if ($hasId === true) {
						$this->addError($attribute, '主键型字段只能有一个');
						$tmp->addError('type', '主键型字段只能有一个');
					}
					$hasId = true;
					break;
				case 'title':
					if ($hasTitle === true) {
						$this->addError($attribute, '标题型字段只能有一个');
						$tmp->addError('type', '标题型字段只能有一个');
					}
					$hasTitle = true;
					break;
				case 'body':
					if ($hasBody === true) {
						$this->addError($attribute, '内容型字段只能有一个');
						$tmp->addError('type', '内容型字段只能有一个');
					}
					$hasBody = true;
					break;
			}
		}
		if ($hasId === false) {
			$this->addError($attribute, '缺少主键型字段');
		}
	}

	public function attributeLabels()
	{
		return array(
			'charset' => '默认字符集',
			'projectName' => '项目名称',
			'indexServer' => '索引服务器',
			'searchServer' => '搜索服务器',
			'fields' => '字段结构',
		);
	}

	public function charsetOptions()
	{
		return array(
			'utf-8' => 'utf-8',
			'gbk' => 'gbk',
			'big5' => 'big5',
		);
	}

	/** 	 
	 * @param XS $xs
	 */
	public function initFromXS($xs)
	{
		$config = $xs->getConfig();
		foreach ($config as $key => $value) {
			if (is_array($value)) {
				$value['name'] = $key;
				$ff = new FieldForm();
				$ff->attributes = $value;
				$this->_fields[] = $ff;
			}
		}
		$this->projectName = $xs->name;
		$this->charset = $xs->defaultCharset;
		$this->indexServer = isset($config['server.index']) ? $config['server.index'] : '';
		$this->searchServer = isset($config['server.search']) ? $config['server.search'] : '';
	}

	public function toIniContent()
	{
		$scheme = new XSFieldScheme();
		foreach ($this->getFields() as $ff) {
			$value = $ff->getAttributes();
			if ($value['name']) {
				$scheme->addField($value['name'], $value);
			}
		}
		$scheme->checkValid(true);
		$content = 'project.name = ' . $this->projectName . "\r\n";
		$content .= 'project.default_charset = ' . $this->charset . "\r\n";
		$content .= 'server.index = ' . $this->indexServer . "\r\n";
		$content .= 'server.search = ' . $this->searchServer . "\r\n\r\n";
		$content .= $scheme;
		return $content;
	}

	public function addEmptyField($num = 2)
	{
		while ($num--) {
			$this->_fields[] = new FieldForm();
		}
	}
}
